<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    

<head>
    <meta charset="utf-8">
    <meta name="keywords" content="汉诺塔游戏（递归与非递归详解）, Five-great的博客">
    <meta name="description" content="汉诺塔游戏​                                                          时间限制: 1 s｜空间限制: 32000 KB
题目描述 Description
汉诺塔问题（又称为河内塔问题">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>汉诺塔游戏（递归与非递归详解） | Five-great的博客</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/css/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

</head>

<body>

<head>
<meta charset="utf-8">

<style>

#canvas1 {
 position: fixed;
 pointer-events:none;//鼠标多层响应事件
        // background: 	#ccc;
		 /*透明度*/
		 width: 100%;
        height: 100%;
     // opacity: 0;
		 overflow: auto;
		 z-index: 999;
}
.hint {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  pointer-events: none;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
  opacity: 0;
  visibility: hidden;
  -webkit-animation: fade-in 1s ease-out 1s forwards, fade-out 1s ease-out 5s forwards;
          animation: fade-in 1s ease-out 1s forwards, fade-out 1s ease-out 5s forwards;
}
.hint > h1 {
  font-size: 32px;
  color: #fff;
}
@-webkit-keyframes fade-in {
  0% {
    opacity: 0;
    visibility: hidden;
  }
  100% {
    opacity: 1;
    visibility: visible;
  }
}
@keyframes fade-in {
  0% {
    opacity: 0;
    visibility: hidden;
  }
  100% {
    opacity: 1;
    visibility: visible;
  }
}
@-webkit-keyframes fade-out {
  0% {
    opacity: 1;
    visibility: visible;
  }
  100% {
    opacity: 0;
    visibility: hidden;
  }
}
@keyframes fade-out {
  0% {
    opacity: 1;
    visibility: visible;
  }
  100% {
    opacity: 0;
    visibility: hidden;
  }
}
</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>
<body>


<canvas id="canvas1" style="whdth: 100%;height: 100%"></canvas>
<!--<div class="hint" id="hint">
  <h1>Click.</h1>
</div>-->

<script>
var _createClass = function () {function defineProperties(target, props) 
{ for (var i = 0; i < props.length; i++) 
 {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) 
 descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}
 return function (Constructor, protoProps, staticProps) 
 {if (protoProps) defineProperties(Constructor.prototype, protoProps);
 if (staticProps) defineProperties(Constructor, staticProps);
 return Constructor;};}();function _classCallCheck(instance, Constructor) 
 {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}
 var getRandom = function getRandom(min, max) {
  return Math.random() * (max - min) + min;
};

var getRandomInt = function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
};

var getRandomColor = function getRandomColor() {
  var colors = [
  'rgba(231, 76, 60, 1)', // red
  'rgba(241, 196, 15, 1)', // yellow
  'rgba(46, 204, 113, 1)', // green
  'rgba(52, 152, 219, 1)', // blue
  'rgba(155, 89, 182, 1)' // purple
  ];

  return colors[getRandomInt(0, colors.length)];
};

// Particle//粒子模块
var
Particle = function () {

  function Particle(system, x, y) {_classCallCheck(this, Particle);
    this.system = system;
    this.universe = this.system.world.universe;
    this.x = x;
    this.y = y;
    this.color = getRandomColor();
    this.life = 1;
    this.aging = getRandom(0.990, 0.999); // 0.99, 0.999 || 0.999, 0.9999

    this.r = getRandomInt(12, 16);//初始粒子半径范围
    this.speed = getRandom(18, 18.5);//粒子爆炸速度范围
    this.velocity = [
    getRandom(-this.speed, this.speed),
    getRandom(-this.speed, this.speed)];

  }_createClass(Particle, [{ key: 'update', value: function update(

    dt) {
      this.life *= this.aging;

      if (
      this.r < 0.1 ||
      this.life === 0 ||
      this.x + this.r < 0 ||
      this.x - this.r > this.universe.width ||
      this.y + this.r < 0 ||
      this.y - this.r > this.universe.height)
      {
        this.system.removeObject(this);
      }

      this.r *= this.life;
      this.x += this.velocity[0];
      this.y += this.velocity[1];
    } }, { key: 'render', value: function render(

    ctx) {
      // Main circle //亮圈模块

      ctx.fillStyle = this.color;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.r*1.2, 0, 2 * Math.PI, false);
      ctx.fill();
      ctx.closePath();

      var r = this.color.match(/([0-9]+)/g)[0];
      var g = this.color.match(/([0-9]+)/g)[1];
      var b = this.color.match(/([0-9]+)/g)[2];

      // Gradient//梯度变化曲线

      var spread = 1.5;
      var gradient = ctx.createRadialGradient(
      this.x, this.y, this.r,
      this.x, this.y, this.r * spread);

      gradient.addColorStop(0, 'rgba(' + r + ', ' + g + ', ' + b + ', 0.5)');
      gradient.addColorStop(1, 'rgba(' + r + ', ' + g + ', ' + b + ', 0)');

      ctx.globalCompositeOperation = 'lighter';
      ctx.fillStyle = gradient;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.r * spread, 0, 2 * Math.PI, false);
      ctx.fill();
      ctx.closePath();
      ctx.globalCompositeOperation = 'source-over';

      // Aberration//偏差

      var offset = this.r * 0.5;
      var color = 'rgba(' + g + ', ' + b + ', ' + r + ', 0.5)';

      ctx.globalCompositeOperation = 'lighter';
      ctx.fillStyle = color;
      ctx.beginPath();
      ctx.arc(this.x + offset, this.y + offset, this.r, 0, 2 * Math.PI, false);
      ctx.fill();
      ctx.closePath();
      ctx.globalCompositeOperation = 'source-over';
    } }]);return Particle;}();



// Crown //水波纹圈模块
var
Crown = function () {

  function Crown(system, x, y) {_classCallCheck(this, Crown);
    this.system = system;
    this.x = x;
    this.y = y;
    this.r = getRandomInt(5, 15); // 5, 20  水波纹圈半径范围
    this.mod = 1.1;
    this.life = 0.5; //水波纹线
    this.aging = getRandom(0.830, 0.899);
    this.speed = getRandom(8, 9);
    this.color = {
      r: getRandomInt(236, 242),
      g: getRandomInt(70, 80),
      b: getRandomInt(50, 70) };


    this.angle1 = Math.PI * getRandom(0, 2);
    this.angle2 = this.angle1 + Math.PI * getRandom(0.3, 0.4);//水波纹圈完整度
  }_createClass(Crown, [{ key: 'update', value: function update(

    dt) {
      this.life *= this.aging;

      if (this.life <= 0.0001) this.system.removeObject(this);

      this.r += Math.abs(1 - this.life) * this.speed;

      this.x1 = this.x + this.r * Math.cos(this.angle1);
      this.y1 = this.y + this.r * Math.sin(this.angle1);

      this.angle3 = this.angle1 + (this.angle2 - this.angle1) / 2;
      this.x2 = this.x + this.r * this.mod * Math.cos(this.angle3);
      this.y2 = this.y + this.r * this.mod * Math.sin(this.angle3);
    } }, { key: 'render', value: function render(

    ctx) {
      var gradient = ctx.createRadialGradient(
      this.x, this.y, this.r * 0.9,
      this.x, this.y, this.r);

      gradient.addColorStop(0, 'rgba(' + this.color.r + ', ' + this.color.g + ', ' + this.color.b + ', ' + this.life + ')');
      gradient.addColorStop(1, 'rgba(' + this.color.r + ', ' + this.color.g + ', ' + this.color.b + ', ' + this.life * 0.5 + ')');

      ctx.fillStyle = gradient;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.r, this.angle1, this.angle2, false);
      ctx.quadraticCurveTo(this.x2, this.y2, this.x1, this.y1);
      ctx.fill();
      ctx.closePath();
    } }]);return Crown;}();



// Explosion //爆炸模块
var
Explosion = function () {

  function Explosion(world, x, y) {_classCallCheck(this, Explosion);
    this.world = world;
    this.x = x;
    this.y = y;
    this.objects = [];

    var particles = getRandomInt(10, 30); // 10, 30 amount of particles//爆炸 粒子数量
    var crowns = particles * getRandom(0.4, 0.5);

    while (crowns-- > 0) {this.addCrown();}
    while (particles-- > 0) {this.addParticle();}
  }_createClass(Explosion, [{ key: 'update', value: function update(

    dt) {
      this.objects.forEach(function (obj) {
        if (obj) obj.update(dt);
      });

      if (this.objects.length <= 0) {
        this.world.clearExplosion(this);
      }
    } }, { key: 'render', value: function render(

    ctx) {
      this.objects.forEach(function (obj) {
        if (obj) obj.render(ctx);
      });
    } }, { key: 'addCrown', value: function addCrown()

    {
      this.objects.push(new Crown(this, this.x, this.y));
    } }, { key: 'addParticle', value: function addParticle()

    {
      this.objects.push(new Particle(this, this.x, this.y));
    } }, { key: 'removeObject', value: function removeObject(

    obj) {
      var index = this.objects.indexOf(obj);

      if (index !== -1) {
        this.objects.splice(index, 1);
      }
    } }]);return Explosion;}();



// World 
var
ConfettiWorld = function () {function ConfettiWorld() 
{_classCallCheck(this, ConfettiWorld);}_createClass(ConfettiWorld, [{ key: 'init', value: function init()

    {
      this.objects = [];
      window.addEventListener('click', this.explode.bind(this));

      // Initial explosion //初始爆炸
      var counter = 0;
      while (counter-- > 0) {
        this.explode({
          clientX: window.event.clientX, //getRandomInt(10, this.universe.width) ,          //this.universe.width / 2,
          clientY: window.event.clientY //getRandomInt(10, 50) //this.universe.height / 2 
		  });

      }
    } }, { key: 'update', value: function update(

    dt) {
      this.objects.forEach(function (obj) {
        if (obj) obj.update(dt);
      });

      var amount = this.objects.reduce(function (sum, explosion) {
        return sum += explosion.objects.length;
      }, 0);
    } }, { key: 'render', value: function render(

    ctx) {
      this.objects.forEach(function (obj) {
        if (obj) obj.render(ctx);
      });
    } }, { key: 'explode', value: function explode(

    event) {
      var x = event.clientX;
      var y = event.clientY;

      this.objects.push(new Explosion(this, x, y));
    } }, { key: 'clearExplosion', value: function clearExplosion(

    explosion) {
      var index = this.objects.indexOf(explosion);

      if (index !== -1) {
        this.objects.splice(index, 1);
      }
    } }]);return ConfettiWorld;}();



// Time
var
Time = function () {

  function Time() {_classCallCheck(this, Time);
    this.now = 0; // current tick time
    this.prev = 0; // prev tick time
    this.elapsed = 0; // elapsed time from last tick
    this.delta = 0; // time from last update
    this.fps = 60; // desired fps
    this.step = 1 / 60; // step duration
  }_createClass(Time, [{ key: 'update', value: function update(

    time) {
      this.now = time;
      this.elapsed = (this.now - this.prev) / 1000;
      this.prev = this.now;
      this.delta += this.elapsed;
    } }, { key: 'raf', value: function raf(

    func) {
      window.requestAnimationFrame(func);
    } }, { key: 'hasFrames', value: function hasFrames()

    {
      return this.delta >= this.step;
    } }, { key: 'processFrame', value: function processFrame()

    {
      this.delta -= this.step;
    } }]);return Time;}();



// Canvas
var
Universe = function () {

  function Universe(element) {_classCallCheck(this, Universe);
    this.el = element;
    this.ctx = this.el.getContext('2d');
    this.pixelRatio = window.devicePixelRatio;
    this.time = new Time();

    this.worlds = {};
    this.world = null; // current state

    this.updateSize();
    window.addEventListener('resize', this.updateSize.bind(this));

    this.addWorld('confetti', ConfettiWorld);
    this.setWorld('confetti');

    this.start();
  }_createClass(Universe, [{ key: 'start', value: function start()

    {
      this.time.raf(this.tick.bind(this));
    } }, { key: 'tick', value: function tick(

    time) {
      this.time.update(time);

      if (this.time.hasFrames()) {
        this.update();
        this.time.processFrame();
      }

      this.render();
      this.time.raf(this.tick.bind(this));
    } }, { key: 'update', value: function update()

    {
      this.world.update(this.time.step);
    } }, { key: 'render', value: function render()

    {
      var gradient = this.ctx.createLinearGradient(0, 0, this.width, this.height);
     // gradient.addColorStop(0, '#34495e');
      //gradient.addColorStop(1, '#2c3e50');

      this.ctx.clearRect(0, 0, this.width, this.height);
      // this.ctx.fillStyle = '#2c3e50'
     // //this.ctx.fillStyle = gradient;
    // // this.ctx.fillRect(0, 0, this.width, this.height);
      this.world.render(this.ctx);
    }

    // Helpers 库
  }, { key: 'updateSize', value: function updateSize()
    {
      this.width = window.innerWidth;
      this.height = window.innerHeight;
      this.el.width = this.width * this.pixelRatio;
      this.el.height = this.height * this.pixelRatio;
      this.el.style.width = window.innerWidth + 'px';
      this.el.style.height = window.innerHeight + 'px';
      this.ctx.scale(this.pixelRatio, this.pixelRatio);
    } }, { key: 'addWorld', value: function addWorld(

    worldName, World) {
      this.worlds[worldName] = new World();
      this.worlds[worldName].universe = this;
      this.worlds[worldName].init();
    } }, { key: 'setWorld', value: function setWorld(

    worldName) {
      this.world = this.worlds[worldName];
    } }]);return Universe;}();



// Main

console.clear();
var element = document.querySelector('#canvas1');
window.Canvas= new Universe(element);</script>

</body>


<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="container">
            <div class="nav-wrapper">
                <div class="brand-logo">
                    <a href="/" class="waves-effect waves-light">
                        
                        <img src="/medias/logo.png" class="logo-img hide-on-small-only">
                        
                        <span class="logo-span">Five-great的博客</span>
                    </a>
                </div>
                <a href="#" data-activates="mobile-nav" class="button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>首页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>归档</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>关于</span>
        </a>
    </li>
    
    <li>
        <a id="toggleSearch" class="waves-effect waves-light">
            <i id="searchIcon" class="mdi-action-search"></i>
        </a>
    </li>

</ul>

<div class="side-nav" id="mobile-nav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Five-great的博客</div>
        <div class="logo-desc">
            
            有事多研究 没事多琢磨
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                首页
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                归档
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                关于
            </a>
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/blinkfox/hexo-theme-matery" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>

    <div class="social-link"><a href="https://github.com/Five-great/Five-great.github.io" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
    <i class="fa fa-github"></i>
</a>
<a href="mailto:https://985540932@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>
<a href="#!" class="tooltipped" data-tooltip="QQ联系我: 985540932" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>

<a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
    <i class="fa fa-rss"></i>
</a>
</div>
</div>

            </div>
        </div>

        
        <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/blinkfox/hexo-theme-matery" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>
</header>

<!doctype html>

<head>
    <meta charset="utf-8" />
    <style>
    *{
        margin: 0;
        padding: 0;
    }
    #canvas{
		 position: fixed;
         background: #ccc;
		 overflow: auto;
		 z-index: -1;
    }
    </style> 

	<canvas id="canvas"></canvas>
	
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>
<body>
    
   
</body>
<script>
    window.requestAnimationFrame = (function(){
        return window.requestAnimationFrame ||
               window.webkitRequestAnimationFrame ||
               window.mozRequestAnimationFrame ||
               function( callback ){
                    window.setTimeout( callback, 1000/2 );
               };
    })();
    var myCanvas = document.getElementById("canvas");
    var ctx = myCanvas.getContext("2d");//getContext 设置画笔
    var num;
    var w,h;
    var duixiang = [];
    var move = {};
    function widthheight(){
        w = myCanvas.width = window.innerWidth;
        h = myCanvas.height = window.innerHeight;
		num= Math.floor(w*h*0.0001);
        for(var i = 0;i < num;i++){
            duixiang[i] = {
                x:Math.random()*w,
                y:Math.random()*h,
                cX:Math.random()*0.6-0.3,
                cY:Math.random()*0.6-0.3,
				R:Math.floor(Math.random()*5)+2,
				//CC:Math.floor(Math.random()*3)+2,
			    r: Math.floor(Math.random() * 254),   
                g: Math.floor(Math.random() * 254),  
                b:Math.floor(Math.random() * 254)   
            } 
           // console.log(duixiang[i])
            Cricle(duixiang[i].x,duixiang[i].y,duixiang[i].R,duixiang[i].r,duixiang[i].g,duixiang[i].b);
			 //Cricle(duixiang[i].x,duixiang[i].y,duixiang[i].R,duixiang[i].CC);
        }
    };widthheight();//获取浏览器的等宽度等高

    function Cricle(x,y,R,r,g,b){
        ctx.save();//保存路径
       if(Math.random()>0.991) {ctx.globalAlpha= 0.9;}//ctx.fillStyle = "#CCC";}//填充的背景颜色
	    else { ctx.globalAlpha=0.47;}
		 
		ctx.fillStyle = "rgb("+ r +","+ g +","+ b +")";
        ctx.beginPath();//开始绘画
		ctx.arc(x,y,R,Math.PI*2,0);//绘画圆 x y 半径（大小） 角度  一个PI 是180 * 2 = 360    真假 0/1 true/false
        ctx.closePath();//结束绘画
        ctx.fill();//填充背景颜色
        ctx.restore();//回复路径
    };Cricle();


    !function draw(){
        ctx.clearRect(0,0,w,h)//先清除画布上的点
        for(var i = 0;i < num;i++){
            duixiang[i].x += duixiang[i].cX;
            duixiang[i].y += duixiang[i].cY;
            if(duixiang[i].x>w || duixiang[i].x<0){
                duixiang[i].cX = -duixiang[i].cX;
            }
            if(duixiang[i].y>h || duixiang[i].y<0){
                duixiang[i].cY = -duixiang[i].cY;
            }
            Cricle(duixiang[i].x,duixiang[i].y,duixiang[i].R,duixiang[i].r,duixiang[i].g,duixiang[i].b);
            //勾股定理判断两点是否连线
            for(var j = i + 1;j < num;j++){
                if( (duixiang[i].x-duixiang[j].x)*(duixiang[i].x-duixiang[j].x)+(duixiang[i].y-duixiang[j].y)*(duixiang[i].y-duixiang[j].y) <= 55*55 ){
                    line(duixiang[i].x,duixiang[i].y,duixiang[j].x,duixiang[j].y,0,i,j)
                }
                if(move.x){
                    if( (duixiang[i].x-move.x)*(duixiang[i].x-move.x)+(duixiang[i].y-move.y)*(duixiang[i].y-move.y) <= 100*100 ){
                        line(duixiang[i].x,duixiang[i].y,move.x,move.y,1,i,1)
                    }
                }
            }
        }
        window.requestAnimationFrame(draw)
    }();

    //绘制线条
    function line(x1,y1,x2,y2,flag,i,j){
        
		if (flag){var color = ctx.createLinearGradient(x1,y1,x2,y2);
					ctx.globalAlpha=0.5;
					color.addColorStop(0,"rgb("+ duixiang[i].r +","+ duixiang[i].g +","+ duixiang[i].b +")");
					color.addColorStop(0.8,"#019ee5");
		        }
				else
				{
				
				var color = ctx.createLinearGradient(x1,y1,x2,y2);
					ctx.globalAlpha=0.9;
					color.addColorStop(0,"rgb("+ duixiang[i].r +","+ duixiang[i].g +","+ duixiang[i].b +")");
					color.addColorStop(1,"rgb("+ duixiang[j].r +","+ duixiang[j].g +","+ duixiang[j].b +")");
				}
        ctx.save();
        ctx.strokeStyle = color;
		ctx.lineWidth = 0.5;
        ctx.beginPath();
        ctx.moveTo(x1,y1);
        ctx.lineTo(x2,y2);
        ctx.stroke();
        //ctx.restore();
    }


    //document.onmousemove = function(e){
     //   move.x = e.clientX;
      //  move.y = e.clientY;
    //}
    //console.log(move)

    window.onresize = function(){
        location.reload();
    }
</script>















<div class="bg-cover post-cover" style="background-image: url('/medias/featureimages/18.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        汉诺塔游戏（递归与非递归详解）
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="container content">

       
          <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            
            <div class="article-tag">
                
                <a href="/tags/ACM/" target="_blank">
                    <span class="chip bg-color">ACM</span>
                </a>
                
                <a href="/tags/水题/" target="_blank">
                    <span class="chip bg-color">水题</span>
                </a>
                
                <a href="/tags/规律/" target="_blank">
                    <span class="chip bg-color">规律</span>
                </a>
                
            </div>
            
			
            <div class="post-info">
                
                <span class="post-cate">
                    <i class="fa fa-bookmark fa-fw icon-category"></i>
                    
                    <a href="/categories/程序设计/" class="post-category" target="_blank">
                        程序设计
                    </a>
                    
                </span>
                

                <span class="post-date">
                    <i class="fa fa-clock-o fa-fw"></i>2018-09-28
                </span>
				<span id="busuanzi_container_page_pv" class="author-info" >
                  &nbsp;&nbsp;&nbsp;&nbsp;<span id="busuanzi_value_page_pv" class="icon fa fa-eye" >&nbsp;</span>次 
                   </span>
            </div>
        </div>
        <hr>
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="汉诺塔游戏"><a href="#汉诺塔游戏" class="headerlink" title="汉诺塔游戏"></a><strong>汉诺塔游戏</strong></h2><p>​                                                          时间限制: 1 s｜空间限制: 32000 KB</p>
<p>题目描述 Description</p>
<p>汉诺塔问题（又称为河内塔问题），是一个大家熟知的问题。在A，B，C三根柱子上，</p>
<p>有n个不同大小的圆盘（假设半径分别为1-n吧），一开始他们都叠在我A上（如图所示），</p>
<p>你的目标是在最少的合法移动步数内将所有盘子从A塔移动到C塔。</p>
<p>游戏中的每一步规则如下：</p>
<hr>
<p>\1. 每一步只允许移动一个盘子（从一根柱子最上方到另一个柱子的最上方）</p>
<p>\2. 移动的过程中，你必须保证大的盘子不能在小的盘子上方</p>
<p>（小的可以放在大的上面，最大盘子下面不能有任何其他大小的盘子）</p>
<p>如对于n=3的情况，一个合法的移动序列式：</p>
<p>1 from A to C</p>
<p>2 from A to B</p>
<p>1 from C to B</p>
<p>3 from A to C</p>
<p>1 from B to A</p>
<p>2 from B to C</p>
<p>1 from A to C</p>
<p>给出一个数n，求出最少步数的移动序列</p>
<p>输入描述 Input Description</p>
<p>一个整数n</p>
<p>输出描述 Output Description</p>
<p>第一行一个整数k，代表是最少的移动步数。</p>
<p>接下来k行，每行一句话，N from X to Y，表示把N号盘从X柱移动到Y柱。X,Y属于{A,B,C}</p>
<p>样例输入 Sample Input</p>
<p>3</p>
<p>样例输出 Sample Output</p>
<p>7</p>
<p>1 from A to C</p>
<p>2 from A to B</p>
<p>1 from C to B</p>
<p>3 from A to C</p>
<p>1 from B to A</p>
<p>2 from B to C</p>
<p>1 from A to C</p>
<p>数据范围及提示 Data Size &amp; Hint</p>
<p>n&lt;=10</p>
<hr>
<p><strong>递归思路分析：</strong></p>
<p>我们设定三个柱子A,B,C。我们的目的是将环从A–&gt;C。（A为起始位置，C为目标位置）</p>
<p>当N=1即一阶时它的路径很简单只需要从A-&gt;C进行移动。</p>
<p>当N=2时我们需要进行三步：</p>
<blockquote>
<h3 id="1-小盘-A-gt-B"><a href="#1-小盘-A-gt-B" class="headerlink" title="1.小盘 A-&gt;B"></a>1.小盘 A-&gt;B</h3></blockquote>
<p>  (假想没有大盘只有小盘，与N=1 的步骤一样，只是目标位置变为了 B)</p>
<blockquote>
<h3 id="2-大盘-A-gt-C"><a href="#2-大盘-A-gt-C" class="headerlink" title="2.大盘 A-&gt;C"></a>2.大盘 A-&gt;C</h3></blockquote>
<p>  (大盘上面的小盘到B去了，与N=1 的步骤一样直接到C )</p>
<blockquote>
<h3 id="3-小盘-B-gt-C"><a href="#3-小盘-B-gt-C" class="headerlink" title="3.小盘 B-&gt;C"></a>3.小盘 B-&gt;C</h3></blockquote>
<p> (大盘到了C,对于小盘而言,C可以看作无盘，与N=1 的步骤一样，只是起始位置变为 B )</p>
<p>   (分解一下，小盘从A通过B作为中间目标再到C。可以这样想 </p>
<p>  小盘下面的大盘目标是C 所以小盘第一次目标则变成B,</p>
<p>  等到大盘到了目标C ,小盘再到C。</p>
<p>  则完成将大小盘按小盘在上大盘在下的要求移到C。）</p>
<p> 当N=3时我们需要进行七步：</p>
<blockquote>
<h3 id="1-小盘-A-gt-C-2-中盘-A-gt-B-3-小盘-C-gt-B"><a href="#1-小盘-A-gt-C-2-中盘-A-gt-B-3-小盘-C-gt-B" class="headerlink" title="1. 小盘 A-&gt;C  2.中盘 A-&gt;B  3.小盘  C-&gt;B"></a>1. 小盘 A-&gt;C  2.中盘 A-&gt;B  3.小盘  C-&gt;B</h3></blockquote>
<p>   (假想没有大盘只有小盘和中盘，与N=2 的步骤一样，只是目标位置变为了 B)</p>
<blockquote>
<h3 id="4-大盘-A-gt-C"><a href="#4-大盘-A-gt-C" class="headerlink" title="4. 大盘 A-&gt;C,"></a>4. 大盘 A-&gt;C,</h3></blockquote>
<p>​    (大盘上面的小盘和中盘都到B去了，与N=1 的步骤一样直接到C )</p>
<blockquote>
<h3 id="5-小盘-B-gt-A-6-中盘-B-gt-C-7-小盘-A-gt-C"><a href="#5-小盘-B-gt-A-6-中盘-B-gt-C-7-小盘-A-gt-C" class="headerlink" title="5. 小盘 B-&gt;A  6.中盘 B-&gt;C  7.小盘  A-&gt;C"></a>5. 小盘 B-&gt;A  6.中盘 B-&gt;C  7.小盘  A-&gt;C</h3></blockquote>
<p>   (大盘到了C,对于小盘和中盘而言,C可以看作无盘，与N=2 的步骤一样，只是起始位置变为了 B )</p>
<p>   (分解一下，大盘想从A去C。但上面压着小盘与中盘 ，</p>
<p>   所以得先把他们移开 并且上面两盘不能移动到C,得移动到B 去</p>
<p>  就相当于N=2时，起始位置A到目标位置B。待大盘移动到C。</p>
<p> 当前在B 的小盘和中盘，完全就是执行N=2 的步骤。从当前起始位置B 到目标位置C.)</p>
<p>如此执行，通过递归方式。代码思路如下：</p>
<p><strong>1. 对于执行最大盘（n） 到C的操作之前，肯定是?把次大盘（n-1）从A移动到 B           2. 执行最大盘（n） 到C的操作           3.对于执行最大盘（n） 到C的操作之后，肯定是?把次大盘（n-1）从B移动到C</strong></p>
<h3 id=""><a href="#" class="headerlink" title=" "></a> </h3><p>   <strong>每次只关心上一层，上上层是到了上一层才考虑的事——递归</strong></p>
<p>题目链接：<a href="http://codevs.cn/problem/3145/" target="_blank" rel="noopener">http://codevs.cn/problem/3145/</a></p>
<hr>
<pre class=" language-cpp"><code class="language-cpp"><span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h></span></span>
<span class="token keyword">void</span> <span class="token function">han</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">char</span> A<span class="token punctuation">,</span> <span class="token keyword">char</span> B<span class="token punctuation">,</span> <span class="token keyword">char</span> C<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d from %c to %c\n"</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> A<span class="token punctuation">,</span> C<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span><span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//第一步  对于执行最大盘（n） 到C的操作之前</span>
    <span class="token function">han</span><span class="token punctuation">(</span>n<span class="token number">-1</span><span class="token punctuation">,</span> A<span class="token punctuation">,</span> C<span class="token punctuation">,</span> B<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//第二步  执行最大盘（n） 到C的操作 </span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d from %c to %c\n"</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> A<span class="token punctuation">,</span> C<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//第三步  对于执行最大盘（n） 到C的操作之后 </span>
    <span class="token function">han</span><span class="token punctuation">(</span>n<span class="token number">-1</span><span class="token punctuation">,</span> B<span class="token punctuation">,</span> A<span class="token punctuation">,</span> C<span class="token punctuation">)</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> n<span class="token punctuation">;</span>
    <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> n<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">han</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> <span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="" alt="点击并拖拽以移动"></p>
<hr>
<p><strong>非递归 思路：</strong></p>
<p> 我们先找找规律：</p>
<p>  当3个盘的时候：</p>
<table>
<thead>
<tr>
<th>1</th>
<th>1:A–&gt;C</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>2:A–&gt;B</td>
</tr>
<tr>
<td>3</td>
<td>1:C–&gt;B</td>
</tr>
<tr>
<td>4</td>
<td>3:A–&gt;C</td>
</tr>
<tr>
<td>5</td>
<td>1:B–&gt;A</td>
</tr>
<tr>
<td>6</td>
<td>2:B–&gt;C</td>
</tr>
<tr>
<td>7</td>
<td>1:A–&gt;C</td>
</tr>
</tbody>
</table>
<p>4个的时候：</p>
<table>
<thead>
<tr>
<th>1</th>
<th>1:A–&gt;B</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>2:A–&gt;C</td>
</tr>
<tr>
<td>3</td>
<td>1:B–&gt;C</td>
</tr>
<tr>
<td>4</td>
<td>3:A–&gt;B</td>
</tr>
<tr>
<td>5</td>
<td>1:C–&gt;A</td>
</tr>
<tr>
<td>6</td>
<td>2:C–&gt;B</td>
</tr>
<tr>
<td>7</td>
<td>1:A–&gt;B</td>
</tr>
<tr>
<td>8</td>
<td>4:A–&gt;C</td>
</tr>
<tr>
<td>9</td>
<td>1:B–&gt;C</td>
</tr>
<tr>
<td>10</td>
<td>2:B–&gt;A</td>
</tr>
<tr>
<td>11</td>
<td>1:C–&gt;A</td>
</tr>
<tr>
<td>12</td>
<td>3:B–&gt;C</td>
</tr>
<tr>
<td>13</td>
<td>1:A–&gt;B</td>
</tr>
<tr>
<td>14</td>
<td>2:A–&gt;C</td>
</tr>
<tr>
<td>15</td>
<td>1:B–&gt;C</td>
</tr>
</tbody>
</table>
<p>仔细研究研究就能发现，1号出现在·1，3，5，7，9步</p>
<p>2号出现在2，6，10，14 步</p>
<p>3号出现在4，12 步</p>
<p>4号在8，步</p>
<p>规律与2^n有关。</p>
<p>我们在研究研究，三个时：</p>
<p>一号盘的行动方式是：</p>
<p>A–&gt;C</p>
<p>C–&gt;B</p>
<p>B–&gt;A</p>
<p>A–&gt;C</p>
<p>二号盘的行动方式是：</p>
<p>A–&gt;B</p>
<p>B–&gt;C</p>
<p>三号盘的行动方式是：</p>
<p>A–&gt;C</p>
<p>四个时：</p>
<p>一号盘的行动方式是：</p>
<p><code>A--&gt;B</code></p>
<p>B–&gt;C</p>
<p>C–&gt;A</p>
<p><code>A--&gt;B</code></p>
<p>B–&gt;C</p>
<p>C–&gt;A</p>
<p><code>A--&gt;B</code></p>
<p>B–&gt;C     </p>
<p>（成一定的周期T=3,当l号盘同最大盘n奇偶性相同，则 执行周期为顺时针，A–&gt;B，B–&gt;C，C–&gt;A</p>
<p>​    否者则 执行周期为逆时针，A–&gt;C，C–&gt;B，B–&gt;A ）</p>
<p>二号盘的行动方式是：</p>
<p>A–&gt;C</p>
<p>C–&gt;B</p>
<p>B–&gt;A</p>
<p>A–&gt;C</p>
<p>三号盘的行动方式是：</p>
<p>A–&gt;B</p>
<p>B–&gt;C</p>
<p>四号盘的行动方式是：</p>
<p>A–&gt;C</p>
<blockquote>
<p>总结下：<br>A号柱有n 个盘子,叫做源柱.移往C 号柱,叫做目的柱.B 号柱叫做中间柱.<br>全部移往C 号柱要f(n) =（2^n）- 1 次.<br>最大盘n 号盘在整个移动过程中只移动一次,n-1 号移动2 次,i 号盘移动<br>2^(n-i)次.<br>1 号盘移动次数最多,每2 次移动一次.<br>第2k+1 次移动的是1 号盘,且是第k+1 次移动1 号盘.<br>第4k+2 次移动的是2 号盘,且是第k+1 次移动2 号盘.</p>
</blockquote>
<blockquote>
<p>第(2^s)k+2^(s-1)次移动的是s 号盘,这时s 号盘已被移动了k+1 次.<br>每2^s 次就有一次是移动s 号盘.<br>第一次移动s 号盘是在第2^(s-1)次.<br>第二次移动s 号盘是在第2^s+2^(s-1)次.<br>第k+1 次移动s 号盘是在第k*2^s+2^(s-1)次.</p>
</blockquote>
<p>A–&gt;B，B–&gt;C，C–&gt;A叫做顺时针方向,A–&gt;C，C–&gt;B，B–&gt;A叫做逆时针方向.<br>最大盘n 号盘只移动一次:A–&gt;C它是逆时针移动.<br>n-1 移动2 次:A–&gt;B，B–&gt;C,是顺时针移动.</p>
<p>代码实现：</p>
<blockquote>
<p>​      枚举 1, 2, 3, 4·····i,  i+1, i+2, ·····步。</p>
</blockquote>
<blockquote>
<p>​       先 获取 第i步移动的几号盘，根据 (2^s)k+2^(s-1)=i,转化一下，满足  i%(2^s) =2^(s-1)  ，令t=2^s;则有i%t=t/2</p>
</blockquote>
<blockquote>
<p>​       再 获得第S盘 第几次移动 ，根据  (2^s)k+2^(s-1)=i,  k=i/(2^s) ,即 k=i/t;</p>
</blockquote>
<blockquote>
<p>​       最后 根据周期T 与奇偶性 确定具体移动的步骤（共6六种）</p>
</blockquote>
<p>代码：</p>
<pre class=" language-cpp"><code class="language-cpp"><span class="token macro property">#<span class="token directive keyword">include</span><span class="token string">&lt;stdio.h></span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">long</span> <span class="token keyword">long</span> i<span class="token punctuation">,</span> res<span class="token punctuation">,</span>t<span class="token punctuation">,</span>k<span class="token punctuation">;</span><span class="token keyword">int</span> n<span class="token punctuation">,</span>s<span class="token punctuation">;</span>
<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">&lt;&lt;</span>n<span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> 
 <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%lld\n"</span><span class="token punctuation">,</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token keyword">for</span><span class="token punctuation">(</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span>res<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">)</span><span class="token punctuation">{</span> 
        <span class="token keyword">for</span><span class="token punctuation">(</span> t<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span>s<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> s<span class="token operator">&lt;=</span> n<span class="token punctuation">;</span> s<span class="token operator">++</span><span class="token punctuation">,</span>t<span class="token operator">*</span><span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token keyword">if</span><span class="token punctuation">(</span> i<span class="token operator">%</span>t <span class="token operator">==</span> t<span class="token operator">/</span><span class="token number">2</span> <span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//i%t=t/2 找 第i步移动的S号盘</span>
          k <span class="token operator">=</span> i<span class="token operator">/</span>t<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//获得第S盘 第几次移动 </span>
            <span class="token keyword">if</span><span class="token punctuation">(</span> n<span class="token operator">%</span><span class="token number">2</span> <span class="token operator">==</span> s<span class="token operator">%</span><span class="token number">2</span> <span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment" spellcheck="true">// 逆时针</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>k<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">3</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d from B to A\n"</span><span class="token punctuation">,</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>k<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">3</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d from A to C\n"</span><span class="token punctuation">,</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>k<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">3</span> <span class="token operator">==</span> <span class="token number">2</span> <span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d from C to B\n"</span><span class="token punctuation">,</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">else</span><span class="token punctuation">{</span><span class="token comment" spellcheck="true">// 逆时针</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>k<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">3</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d from C to A\n"</span><span class="token punctuation">,</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>k<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">3</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d from A to B\n"</span><span class="token punctuation">,</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>k<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">3</span> <span class="token operator">==</span> <span class="token number">2</span> <span class="token punctuation">)</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d from B to C\n"</span><span class="token punctuation">,</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
     <span class="token punctuation">}</span>
 <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>更多详情请到：<a href="https://blog.csdn.net/qq_41923622/article/details/82829067" target="_blank" rel="noopener">Five-菜鸟级</a></p>

            </div>
            <hr/>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
		z-index: 1000;
		position: float;
		
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
	
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
	
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
	
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff;
        background-color: #22AB38;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff;
        background-color: #019FE8;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a class="reward-link btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs">
                        <li class="tab wechat-tab waves-effect waves-light"><a class="active" href="#wechat">微信</a></li>
                        <li class="tab alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                    </ul>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('#reward .reward-link').on('click', function () {
            $('#rewardModal').openModal();
        });

        $('#rewardModal .close').on('click', function () {
            $('#rewardModal').closeModal();
        });
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">转载请注明: </span>
                    <a href="https://five-great.coding.me" class="b-link-green">Five-great的博客</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/han-nuo-ta-you-xi.html" class="b-link-green">汉诺塔游戏（递归与非递归详解）</a>
                </p>
            </div>
        </div>
    </div>

	
	    
        

<link rel="stylesheet" href="/libs/gitment/gitment-default.css">
<link rel="stylesheet" href="/css/gitment.css">
 <div class="gitment-card card" data-aos="fade-up">
   
    <div></div>
<!-- 来必力City版安装代码 -->
<div id="lv-container" data-id="city" data-uid="MTAyMC80MDE5NS8xNjcyMg==">
	
	
	<script type="text/javascript">
   (function(d, s) {
       var j, e = d.getElementsByTagName(s)[0];

       if (typeof LivereTower === 'function') { return; }

       j = d.createElement(s);
       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       j.async = true;

       e.parentNode.insertBefore(j, e);
   })(document, 'script');
	</script>
<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
</div>
<!-- City版安装代码已完成 -->

  </div>
        
    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">上一篇</div>
            <div class="card">
                <a href="/tong-shi-bu-shu-coding-yu-github.html">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/3.jpg" class="responsive-img" alt="同时部署coding与github">
                        
                        <span class="card-title">同时部署coding与github</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">前言之前我是把hexo托管在github，但是毕竟github是国外的，访问速度上还是有点慢，所以想也部署一套在国内的托管平台，所以就决定部署到coding。 查询了多方资料，终于鼓捣出了本地一次部署，同时更新到github以及coding</div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2018-09-28
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/前端/" class="post-category" target="_blank">
                                    前端
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Hexo/" target="_blank">
                        <span class="chip bg-color">Hexo</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">下一篇</div>
            <div class="card">
                <a href="/hello.html">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/2.jpg" class="responsive-img" alt="Hello">
                        
                        <span class="card-title">Hello</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">#你好这是我第一篇文章:)
</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2018-09-26
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-user fa-fw"></i>
                            LHL
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>
                 

</main>


   <footer class="page-footer bg-color">

	<div>
	    <li class="col s12 m8 l8 copy-right">
	 友链：
	      <a href="https://blog.csdn.net/qq_41923622" target="_blank"><i class="fa fa-link fa-lg"></i> Five-菜鸟级  </a> 	
          &nbsp;&nbsp;<a href="https://blinkfox.github.io/" target="_blank"><i class="fa fa-link fa-lg"></i> 闪烁之狐</a>  		
          &nbsp;&nbsp;<a href="http://www.greyking.cn/" target="_blank"><i class="fa fa-link fa-lg"></i> 浩瀚渊海</a>  				  
		<span id="busuanzi_container_site_uv" style="float:right"> 
  恭喜您成为第 <span id="busuanzi_value_site_uv"></span>位访客
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </span>
      </span>
	  </li>
		
	</div>
	<div class="container row center-align">
        <div class="col s12 m8 10 copy-right">
           本站由&copy;<a href="http://#" target="_blank">LHL</a>基于
            <a href="https://hexo.io/" target="_blank">Hexo</a>与
            <a href="https://github.com/" target="_blank">github</a>搭建.
        </div>
        <div class="col s12 m4 5 social-link">
            <a href="https://github.com/Five-great/Five-great.github.io" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
                <i class="fa fa-github fa-lg"></i>
            </a>
            <a href="https://985540932@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
                <i class="fa fa-envelope fa-lg"></i>
            </a>
            <a href="#!" class="tooltipped" data-tooltip="QQ: 985540932" data-position="top" data-delay="50">
                <i class="fa fa-qq fa-lg"></i>
            </a>
			<a href="#!" class="tooltipped" data-tooltip="微信:qq985540932" data-position="top" data-delay="50">
                <i class="fa  fa-wechat fa-lg"></i>
            </a>
        </div>
    </div>
</footer>
<div class="progress-bar"></div>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?d62256055258ddb12716821c285aadab";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
           


<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>



<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title">搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input" autofocus="">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-double-up"></i>
    </a>
</div>


<script src="/libs/materialize/js/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>
</body>
</html>